import 'package:flutter/material.dart';

class GenderSelector extends StatefulWidget {
  final String initialGender;
  final Function(String) onGenderChanged;

  const GenderSelector({Key? key, required this.initialGender, required this.onGenderChanged}) : super(key: key);

  @override
  _GenderSelectorState createState() => _GenderSelectorState();
}

class _GenderSelectorState extends State<GenderSelector> {
  late String _gender;

  @override
  void initState() {
    super.initState();
    _gender = widget.initialGender;
  }

  @override
  Widget build(BuildContext context) {
    return DropdownButtonFormField<String>(
      value: _gender,
      decoration: const InputDecoration(
        labelText: '性别',
        icon: Icon(Icons.transgender),
      ),
      items: <String>['男', '女'].map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: Text(value),
        );
      }).toList(),
      onChanged: (String? newValue) {
        if (newValue != null) {
          setState(() {
            _gender = newValue;
          });
          widget.onGenderChanged(newValue);
        }
      },
    );
  }
}
